<template>
  <div class="register-container">
    <div class="register-form-wrapper">
      <h1 class="register-title">注册</h1>
      <p class="register-subtitle">创建您的账户以开始使用</p>
      
      <form @submit.prevent="handleRegister" class="register-form">
        <div class="form-group">
          <div class="input-wrapper">
            <svg class="icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M20 21V19C20 17.9391 19.5786 16.9217 18.8284 16.1716C18.0783 15.4214 17.0609 15 16 15H8C6.93913 15 5.92172 15.4214 5.17157 16.1716C4.42143 16.9217 4 17.9391 4 19V21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M12 11C14.2091 11 16 9.20914 16 7C16 4.79086 14.2091 3 12 3C9.79086 3 8 4.79086 8 7C8 9.20914 9.79086 11 12 11Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <input
              v-model="formData.username"
              type="text"
              placeholder="用户名"
              class="form-input"
              required
            />
          </div>
        </div>
        
        <div class="form-group">
          <div class="input-wrapper">
            <svg class="icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M12 8V12L14 14" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <input
              v-model="formData.email"
              type="email"
              placeholder="邮箱"
              class="form-input"
              required
            />
          </div>
        </div>
        
        <div class="form-group">
          <div class="input-wrapper">
            <svg class="icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M16 21V19C16 17.9391 15.5786 16.9217 14.8284 16.1716C14.0783 15.4214 13.0609 15 12 15H8C6.93913 15 5.92172 15.4214 5.17157 16.1716C4.42143 16.9217 4 17.9391 4 19V21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M12 12C13.6569 12 15 10.6569 15 9C15 7.34315 13.6569 6 12 6C10.3431 6 9 7.34315 9 9C9 10.6569 10.3431 12 12 12Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M8.5 8.5L7.5 7.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M16.5 8.5L15.5 7.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <input
              v-model="formData.password"
              :type="showPassword ? 'text' : 'password'"
              placeholder="密码"
              class="form-input"
              required
            />
            <button
              type="button"
              class="toggle-password"
              @click="togglePassword"
              aria-label="Toggle password visibility"
            >
              <svg class="icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M12 16v-4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M12 8h.01" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              </svg>
            </button>
          </div>
        </div>
        
        <div class="form-group">
          <div class="input-wrapper">
            <svg class="icon" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M16 21V19C16 17.9391 15.5786 16.9217 14.8284 16.1716C14.0783 15.4214 13.0609 15 12 15H8C6.93913 15 5.92172 15.4214 5.17157 16.1716C4.42143 16.9217 4 17.9391 4 19V21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M12 12C13.6569 12 15 10.6569 15 9C15 7.34315 13.6569 6 12 6C10.3431 6 9 7.34315 9 9C9 10.6569 10.3431 12 12 12Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M8.5 8.5L7.5 7.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M16.5 8.5L15.5 7.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <input
              v-model="formData.confirmPassword"
              :type="showPassword ? 'text' : 'password'"
              placeholder="确认密码"
              class="form-input"
              required
            />
          </div>
        </div>
        
        <button
          type="submit"
          class="register-button"
          :disabled="loading || !isFormValid"
        >
          {{ loading ? '注册中...' : '注册' }}
        </button>
        
        <div class="form-footer">
          <span class="login-text">已有账号？</span>
          <router-link to="/login" class="login-button">登录</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
import { reactive, ref } from 'vue';
import { useUserStore } from '@/stores/user';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';

export default {
  name: 'RegisterView',
  setup() {
    const userStore = useUserStore();
    const router = useRouter();
    const { loading, error } = storeToRefs(userStore);
    
    // 使用reactive创建响应式表单数据
    const formData = reactive({
      username: '',
      email: '',
      password: '',
      confirmPassword: ''
    });
    
    const showPassword = ref(false);
    
    return {
      formData,
      showPassword,
      loading,
      error,
      userStore,
      router
    };
  },
  computed: {
    isFormValid() {
      // 简单的表单验证
      return this.formData.username && 
             this.formData.email && 
             this.formData.password && 
             this.formData.password === this.formData.confirmPassword &&
             this.formData.password.length >= 6;
    }
  },
  methods: {
    togglePassword() {
      this.showPassword = !this.showPassword;
    },
    async handleRegister() {
      // 表单验证
      if (this.formData.password !== this.formData.confirmPassword) {
        alert('两次输入的密码不一致');
        return;
      }
      
      if (this.formData.password.length < 6) {
        alert('密码长度至少为6位');
        return;
      }
      
      this.userStore.clearError();
      
      try {
        // 准备注册数据（不包含confirmPassword）
        const registerData = {
          username: this.formData.username,
          email: this.formData.email,
          password: this.formData.password
        };
        
        await this.userStore.register(registerData);
        
        // 显示成功消息
        alert('注册成功！请登录');
        
        // 跳转到登录页面
        this.router.push('/login');
      } catch (err) {
        // 错误已在store中处理
        console.error('Registration failed:', err)
        alert(this.error || '注册失败，请稍后再试');
      }
    }
  },
  beforeUnmount() {
    // 组件卸载时清除错误
    this.userStore.clearError();
  }
};
</script>

<style scoped>
/* 注册容器 - 全屏flex布局 */
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('@/assets/img/psychology.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 20px;
  width: 100%;
  box-sizing: border-box;
  margin: 0;
  position: relative;
}

/* 注册表单包装器 - 响应式卡片 */
.register-form-wrapper {
  background: linear-gradient(135deg, #d4f0f0 0%, #fccbfa 100%);
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  max-width: 400px;
  width: 100%;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.register-form-wrapper::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(45deg, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 50%, rgba(255,255,255,0.1) 100%);
  transform: rotate(45deg);
  animation: shimmer 2s infinite;
}

@keyframes shimmer {
  0% {
    transform: translateX(-100%) rotate(45deg);
  }
  100% {
    transform: translateX(100%) rotate(45deg);
  }
}

.register-title {
  font-size: 28px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 8px;
  color: #333;
}

.register-subtitle {
  font-size: 14px;
  text-align: center;
  color: #666;
  margin-bottom: 30px;
}

.register-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  width: 100%;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  padding: 0 15px;
  transition: all 0.3s ease;
}

.input-wrapper:focus-within {
  border-color: #61dafb;
  box-shadow: 0 0 0 3px rgba(97, 218, 251, 0.1);
}

.icon {
  width: 18px;
  height: 18px;
  color: #888;
  margin-right: 10px;
  flex-shrink: 0;
}

.form-input {
  width: 100%;
  padding: 14px 10px;
  border: none;
  outline: none;
  background: transparent;
  font-size: 16px;
  color: #333;
}

.form-input::placeholder {
  color: #aaa;
}

.toggle-password {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.3s ease;
}

.toggle-password:hover {
  opacity: 0.8;
}

.register-button {
  background: linear-gradient(135deg, #61dafb 0%, #4fc0e8 100%);
  color: white;
  border: none;
  padding: 14px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.register-button:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(97, 218, 251, 0.3);
}

.register-button:active:not(:disabled) {
  transform: translateY(0);
}

.register-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.form-footer {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin-top: 5px;
  text-align: center;
}

.login-text {
  font-size: 14px;
  color: #666;
}

.login-button {
  background: rgba(255, 255, 255, 0.9);
  color: #61dafb;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: bold;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.3s ease;
}

.login-button:hover {
  background: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* H5端响应式适配 */
@media (max-width: 768px) {
  .register-container {
    padding: 10px;
  }
  
  .register-form-wrapper {
    padding: 30px 20px;
    max-width: 100%;
  }
  
  .register-title {
    font-size: 24px;
  }
  
  .register-subtitle {
    font-size: 13px;
  }
  
  .register-form {
    gap: 15px;
  }
  
  .form-input {
    padding: 12px 10px;
    font-size: 15px;
  }
  
  .register-button {
    padding: 12px;
    font-size: 15px;
  }
  
  .form-footer {
    flex-direction: column;
    gap: 10px;
  }
  
  .login-text,
  .login-button {
    font-size: 13px;
  }
}
</style>